package iscript.gameFramework.NetService.data 
{
	import iscript.coreFramework.debug.ArthropodDebug;
	
	public class WatchServiceEventList 
	{
		
		public function WatchServiceEventList() 
		{
			
		}
		 
		 
		 private var _data:Vector.<WatchServiceItem> = new Vector.<WatchServiceItem>();
		 
		 public function get data():Vector.<WatchServiceItem> { return _data; }
		 
		 /**
		  * 添加包监听
		  * @param	commandName
		  * @param	keyFn
		  * @param	processFn
		  */
		 public function add(commandName:String,keyFn:Function, processFn:Function):void {
			      var item:WatchServiceItem = new WatchServiceItem();
				      item.callbackFn = keyFn;
					  item.processFn = processFn;
					  item.commandName = commandName;
			          _data.push(item);
					  
			 } 
			 
		 public function getWatchServiceItem(commandName:String, callbackFn:Function):Vector.<WatchServiceItem> {
			        var data:Vector.<WatchServiceItem> = new Vector.<WatchServiceItem>();
					for ( var i:int = 0; i < _data.length; i++) 
					{
						var  item:WatchServiceItem = WatchServiceItem(_data[i])
						if( commandName ==item.commandName &&  callbackFn == item.callbackFn){
						   data.push(item);
						}
					}
				   return data;
			 }	 
		/**
		 * 用户是否注册了包
		 * @param	commandName
		 * @return
		 */
		 public function  isRegisterBycommandName(commandName:String):Boolean {
			      for each ( var item :WatchServiceItem in _data) {
					     if (item.commandName == commandName) return true;
					  }
			      return false;
			 }	 
			 
			 
		/**
		 * 移除包监听
		 * @param	commandName
		 * @param	callbackFn
		 */
		 public function remove(commandName:String, callbackFn:Function):void {
			    if (_data.length == 0) return;
			    var removeIndexList:Vector.<int> = new Vector.<int>();
				var  item:WatchServiceItem;
				var i:int;
			    for ( i = 0; i < _data.length; i++) 
				{
					item = WatchServiceItem(_data[i])
					
					if ( commandName == item.commandName &&  callbackFn == item.callbackFn) {
					   removeIndexList.push(i);
					}
				}
				removeIndexList.reverse();
				
				for ( i = 0; i < removeIndexList.length; i++) 
				{
					_data.splice(removeIndexList[i], 1);
				}				
			 }
		 
	}

}